<?php

/**
 * Copyright              [HD框架] (C)2011-2012 houdunwang ，Inc. 
 * Encoding               UTF-8
 * Version                 $Id: appControl.php      2012-5-13  9:32:35
 * @author                向军
 * Link                       http://www.houdunwang.com       
 * E-mail                    houdunwang@gmail.com
 */
class appControl extends setupControl {

    function index() {
        $this->display();
    }

    function setup() {
        $post = $_POST;
        $file = '../' . trim($post['filename'], '.php') . '.php';
        $appgroup = empty($post['appgroup']) ? '' : "define('APP_GROUP','{$post['appgroup']}');//应用组目录 \n";
        $appname = empty($post['appname']) ? '' : "define('APP_NAME','{$post['appname']}');//应用名 \n";
        $compile = $post['compile'] ? "define('COMPILE',true);//是否生成核心编译文件\n" : "define('COMPILE',false);//是否生成核心编译文件 \n";
        $str = '<?php ' . "\n //www.houdunwang.com 向军\n " . $appgroup .
                $appname .
                $compile .
                "include './hdphp/hdphp.php'; //HD框架核心包 \n" .
                "HD::run();//运行\n " . ' ?>';
        $appgroup = empty($post['appgroup']) ? $post['appgroup'] : $post['appgroup'] . '/';
        $url = dirname(__ROOT__) . '/' . str_replace('../', '', $file);
        $appDir = PATH_ROOT . '/' . $appgroup . $post['appname'];
       //删除缓存
        $cachefiles = glob('../temp/*');
        foreach ($cachefiles as $v) {
            $fileName = basename($v);
            if (in_array($fileName, array("log", "session"))) {
                continue;
            }
            dir::del($v);
        }
        if (is_dir($appDir)) {
            $this->error("应用{$post['appname']}已经创建，请删除后再创建");
        }
        if (is_file($file)) {
            $this->error("单入口文件已经存在");
        }

        if (file_put_contents($file, $str)) {
            $this->success("单入口文件创建成功", $url);
            $this->go($url);
        } else {
            $this->error('文件创建失败，请检查目录权限');
        }
    }

}

?>
